import request from '@/utils/request'
import type {
  GetUserListApiParams,
  GetUserListApiResponse,
  UserItem,
  GetUserRoleResponse,
} from './model/userModel'

enum Api {
  getUserList = '/admin/acl/user',
  saveUser = '/admin/acl/user/save',
  updateUser = '/admin/acl/user/update',
  removeUser = '/admin/acl/user/remove',
  batchRemoveUser = '/admin/acl/user/batchRemove',
  assignUser = '/admin/acl/user/doAssign',
  getUserRole = '/admin/acl/user/toAssign',
}

// 获取用户分页列表
export const getUserListApi = ({
  page,
  limit,
  username,
}: GetUserListApiParams) => {
  return request.get<any, GetUserListApiResponse>(
    `${Api.getUserList}/${page}/${limit}`,
    {
      params: {
        username,
      },
    }
  )
}

// 添加用户
export const saveUserApi = (data: UserItem) => {
  return request.post<any, null>(Api.saveUser, data)
}

// 修改用户
export const updateUserApi = (data: UserItem) => {
  return request.put<any, null>(Api.updateUser, data)
}

// 删除用户
export const removeUserApi = (id: number) => {
  return request.delete<any, null>(`${Api.removeUser}/${id}`)
}

// 批量删除用户
export const batchRemoveUserApi = (idList: number[]) => {
  return request.delete<any, null>(Api.batchRemoveUser, {
    data: idList, // 请求体参数
  })
}

// 给用户分配角色
export const assignUserRoleApi = (userId: number, roleId: number[]) => {
  // return request.post<any, null>(`${Api.assignUser}?userId=${userId}&roleId=${roleId}`);
  return request.post<any, null>(
    Api.assignUser,
    {}, // 请求体参数
    {
      // 其他配置项
      params: {
        userId,
        roleId: roleId.join(','),
      },
    }
  )
}

// 获取用户的角色数据
export const getUserRoleApi = (adminId: number) => {
  return request.get<any, GetUserRoleResponse>(`${Api.getUserRole}/${adminId}`)
}
